/*
 * 
 * 
 *
 * 
 */

#include <common.h>
#include <bootretry.h>
#include <cli.h>
#include <command.h>
#include <console.h>
#include <dm.h>
#include <edid.h>
#include <environment.h>
#include <errno.h>
#include <i2c.h>
#include <malloc.h>
#include <asm/byteorder.h>
#include <linux/compiler.h>
#include <pwm.h>
/*↓*↓*↓*↓*↓*↓*↓**nova_lcd_logo**↓*↓*↓*↓*↓*↓*↓*/
#include <spi.h>
#include <asm/gpio.h>
#include <asm/unaligned.h>
#include <bmp_layout.h>
#include <mapmem.h>
#include <part.h>
#include <config.h>
#include <image.h>
#include <linux/ctype.h>
#include <ext4fs.h>
#include <linux/stat.h>
#include <fs.h>
#include <linux/math64.h>
/*↑*↑*↑*↑*↑*↑*↑**nova_lcd_logo**↑*↑*↑*↑*↑*↑*↑*/



#include <linux/delay.h>

/*通过para 来实现液晶的背光控制*/

/*备注,两片LCD均采用
  IM3~IM0 =0110  
  4-line serial interface Ⅰ   SDA: in/out

  4-line serial interface Ⅰ 
  Pin Name  Description 
  CSX--Chip selection signal 
  WRX-- 
      Data is regarded as a command when WRX is low 
      Data is regarded as a parameter or data when WRX is high 
  DCX--Clock signal 
  SDA--Serial input/output data 
*/

/*主显示屏使用的是-----SPI2---J16A*/
#define MAIN_RST_L           run_command("gpio clear E30", 0);   //E30 --RK3288_GPIO4_D6 LCD_RST   
#define MAIN_RST_H           run_command("gpio set E30", 0);     
#define MAIN_BLK_L           run_command("gpio clear E26", 0);   //E26 --RK3288_GPIO4_D2 LCD_BKL_EN      
#define MAIN_BLK_H           run_command("gpio set E26", 0);   
#define MAIN_WRX_L           run_command("gpio clear E31", 0);   //E31 --RK3288_GPIO4_D7 LCD_RS    
#define MAIN_WRX_H           run_command("gpio set E31", 0);   
/*SPI2*/
#define MAIN_SPI_CS_L        //run_command("gpio clear H7", 0);    //H7 --RK3288_SPI2_CSN0 --GPIO8_A7
#define MAIN_SPI_CS_H        //run_command("gpio set H7", 0);      
#define MAIN_SPI_CLK_L       run_command("gpio clear H6", 0);    //H6 --RK3288_SPI2_CLK  --GPIO8_A6
#define MAIN_SPI_CLK_H       run_command("gpio set H6", 0);      
#define MAIN_SPI_MISO_L      run_command("gpio clear H8", 0);    //H8 --RK3288_SPI2_MISO --GPIO8_B0    
#define MAIN_SPI_MISO_H      run_command("gpio set H8", 0);   
#define MAIN_SPI_MOSI_L      run_command("gpio clear H9", 0);    //H9 --RK3288_SPI2_MOSI --GPIO8_B1   
#define MAIN_SPI_MOSI_H      run_command("gpio set H9", 0);   

/*RGB 模式下的驱动配置-----使用的SPI1 */
#define PREVIEW_RST_L        run_command("gpio clear E22", 0);   //E22--LCD_RSTn--RK3288_GPIO4_C6    
#define PREVIEW_RST_H        run_command("gpio set E22", 0);          
#define PREVIEW_BLK_L        ;                                   //未连接管脚
#define PREVIEW_BLK_H        ;          
#define PREVIEW_WRX_L        run_command("gpio clear E18", 0);   //E18--LCD_WRX --RK3288_GPIO4_C2     
#define PREVIEW_WRX_H        run_command("gpio set E18", 0);   
/*SPI1*/                                                                          
#define PREVIEW_SPI_CS_L     //run_command("gpio clear G13", 0);    //G13 --RK3288_SPI1_CSN0-- GPIO7_B5
#define PREVIEW_SPI_CS_H     //run_command("gpio set G13", 0);       
#define PREVIEW_SPI_CLK_L    run_command("gpio clear G12", 0);    //G12 --RK3288_SPI1_CLK -- GPIO7_B4  
#define PREVIEW_SPI_CLK_H    run_command("gpio set G12", 0);       
#define PREVIEW_SPI_MISO_L   run_command("gpio clear G14", 0);    //G14 --RK3288_SPI1_MISO-- GPIO7_B6     

#define PREVIEW_SPI_MISO_H   run_command("gpio set G14", 0);   
#define PREVIEW_SPI_MOSI_L   run_command("gpio clear G15", 0);    //G15 --RK3288_SPI1_MOSI-- GPIO7_B7   
#define PREVIEW_SPI_MOSI_H   run_command("gpio set G15", 0);   
















/********SPI驱动代码***********************************************************
 *函数名：do_spi_xfer_val
 *参  数：bus:spi2, cs:0, val:要传的值。 length：要传值的长度
 *返回值：void无
 *作  用：发送数据和命令大小的值
 ******************************************************************************/
static int do_spi_xfer_val(int bus, int cs, u8 val, int length) {
	struct spi_slave *slave;
	int ret = 0;

#ifdef CONFIG_DM_SPI
	char name[30], *str;
	struct udevice *dev;

	snprintf(name, sizeof(name), "generic_%d:%d", bus, cs);
	str = strdup(name);
	if (!str)
		return -ENOMEM;
	ret = spi_get_bus_and_cs(bus, cs, 15*1000*1000, 0, "spi_generic_drv",
				 str, &dev, &slave);
	if (ret)
		return ret;
#else
	slave = spi_setup_slave(bus, cs, 15*1000*1000, 0);
	if (!slave) {
		printf("Invalid device %d:%d\n", bus, cs);
		return -EINVAL;
	}
#endif

	ret = spi_claim_bus(slave);
	if (ret)
		goto done;
	
	ret = spi_xfer(slave, length*8, &val, NULL,
		       SPI_XFER_BEGIN | SPI_XFER_END);
#ifndef CONFIG_DM_SPI
	/* We don't get an error code in this case */
	if (ret)
		ret = -EIO;
#endif
	if (ret) {
		printf("Error %d during SPI transaction\n", ret);
	} else {
		/*nothing to do*/
	}
done:
	spi_release_bus(slave);
#ifndef CONFIG_DM_SPI
	spi_free_slave(slave);
#endif

	return ret;
}

static int do_spi_xfer_vals(int bus, int cs, u8 *buf, int length) {
	struct spi_slave *slave;
	int ret = 0;

#ifdef CONFIG_DM_SPI
	char name[30], *str;
	struct udevice *dev;

	snprintf(name, sizeof(name), "generic_%d:%d", bus, cs);
	str = strdup(name);
	if (!str)
		return -ENOMEM;
	ret = spi_get_bus_and_cs(bus, cs, 15*1000*1000, 0, "spi_generic_drv",
				 str, &dev, &slave);
	if (ret)
		return ret;
#else
	slave = spi_setup_slave(bus, cs, 15*1000*1000, 0);
	if (!slave) {
		printf("Invalid device %d:%d\n", bus, cs);
		return -EINVAL;
	}
#endif

	ret = spi_claim_bus(slave);
	if (ret)
		goto done;
	
	ret = spi_xfer(slave, length*8, buf, NULL,
		       SPI_XFER_BEGIN | SPI_XFER_END);
#ifndef CONFIG_DM_SPI
	/* We don't get an error code in this case */
	if (ret)
		ret = -EIO;
#endif
	if (ret) {
		printf("Error %d during SPI transaction\n", ret);
	} else {
		/*nothing to do*/
	}
done:
	spi_release_bus(slave);
#ifndef CONFIG_DM_SPI
	spi_free_slave(slave);
#endif

	return ret;
}



/*屏幕信息配置  */
#define LCD_X_MAX   240 /*屏幕X坐标最大范围*/
#define LCD_Y_MAX   320 /*屏幕Y坐标最大范围*/

#define PIC_WIDTH  	240 /*图片宽*/
#define PIC_HEIGHT	320 /*图片高*/


/******************************************************************************
 *函数名：spi_write_comd
 *参  数：要写入的命令值
 *返回值：void无
 *作  用：写命令
 ******************************************************************************/
void spi_write_comd(unsigned char spi_num,unsigned char val) {
	if(spi_num==1){      
    PREVIEW_WRX_L;/*命令*/
    PREVIEW_SPI_CS_L; /*CS 0*/
  }else if(spi_num==2){ 
  	MAIN_WRX_L;/*命令*/
  	MAIN_SPI_CS_L;    /*CS 0*/
	}
	// 发送接下来的8位数据
	do_spi_xfer_val(spi_num, 0, val, 1);
	
	if(spi_num==1){ 
    PREVIEW_SPI_CS_H;/*CS 1*/
	}else if(spi_num==2){ 
  	MAIN_SPI_CS_H;/*CS 1*/
	}
}

/******************************************************************************
 *函数名：spi_write_data
 *参  数：要写入的数据值
 *返回值：void无
 *作  用：写数据
 ******************************************************************************/
void spi_write_data(unsigned char spi_num,unsigned char val) {
	if(spi_num==1){      
    PREVIEW_WRX_H;/*数据*/
    PREVIEW_SPI_CS_L; /*CS 0*/
  }else if(spi_num==2){ 
  	MAIN_WRX_H;/*数据*/
  	MAIN_SPI_CS_L;    /*CS 0*/
	}
	// 发送接下来的8位数据
	do_spi_xfer_val(spi_num, 0, val, 1);
	
	if(spi_num==1){ 
    PREVIEW_SPI_CS_H;/*CS 1*/
	}else if(spi_num==2){ 
  	MAIN_SPI_CS_H;/*CS 1*/
	}
}
/*****************************************************************************
 *函数名：lcd_set_pos
 *参  数：xs:x轴起始位置；xe:x轴结束位置；ys:y轴起始位置；ye:y轴结束位置；
 *返回值：void无
 *作  用：设定屏幕显示位置（？是否需要设置某个寄存器？）
 ******************************************************************************/
void lcd_set_pos(unsigned char spi_num,unsigned int xs, unsigned int xe,unsigned int ys, unsigned int ye) {
	spi_write_comd(spi_num,0x2A);
	spi_write_data(spi_num,xs>>8);
	spi_write_data(spi_num,xs&0xff);
	spi_write_data(spi_num,xe>>8);
	spi_write_data(spi_num,xe&0xff);
	
	spi_write_comd(spi_num,0x2B);
	spi_write_data(spi_num,ys>>8);
	spi_write_data(spi_num,ys&0xff);
	spi_write_data(spi_num,ye>>8);
	spi_write_data(spi_num,ye&0xff);
	
	spi_write_comd(spi_num,0x2C);/*并配置写入ram命令,之后的数据则直接写入显存ram了*/
}



unsigned int reverse_8bits(unsigned char value)
{
    int answer = 0,i = 0;
    for ( ; i < 8; ++i)
    {   
        if (value & 1)
            answer |= 1;
        value >>= 1;
        answer <<= 1;
    }
    return answer;
}
unsigned int reverse_16bits(unsigned short value)
{
    int answer = 0,i = 0;
    for ( ; i < 16; ++i)
    {   
        if (value & 1)
            answer |= 1;
        value >>= 1;
        answer <<= 1;
    }
    return answer;
}

unsigned int reverse_32bits(unsigned int value)
{
    int answer = 0,i = 0;
    for ( ; i < 31; ++i)
    {   
        if (value & 1)
            answer |= 1;
        value >>= 1;
        answer <<= 1;
    }
    return answer;
}


void lcd_clear(unsigned char spi_num) {
  u8 R5,G6,B5;
  int i,j;
  int dis_date;
  
	//lcd_set_pos(spi_num,((COL-PIC_HEIGHT)/2), ((COL-PIC_HEIGHT)/2)+PIC_HEIGHT-1, 0, PIC_WIDTH-1);
	lcd_set_pos(spi_num,0, 319, 0,239);
	if(spi_num==1){      
    PREVIEW_WRX_H;/*数据*/
    PREVIEW_SPI_CS_L; /*CS 0*/
  }else if(spi_num==2){ 
  	MAIN_WRX_H;/*数据*/
  	MAIN_SPI_CS_L;    /*CS 0*/
	}
  if(spi_num==2)/*液晶是主屏幕,565格式*/
	{
	  
    for (i=0; i<240; i++) {
      for (j=0; j<320; j++) {

        R5=0X00;
        G6=0X00;
        B5=0X00;

        dis_date = ( (int)B5<<11  ) | ((int)G6<<5) | ((int)R5>>3);
        do_spi_xfer_val(spi_num, 0, ((dis_date>>8)), 1);  /*高位*/ /*var ==对应的颜色是BGR*/
        do_spi_xfer_val(spi_num, 0, ((dis_date)), 1);     /*低位*/
      }
   }
	}else if(spi_num==1) /*预监屏幕 666格式*/
	{

	}
	
	if(spi_num==1){ 
    PREVIEW_SPI_CS_H;/*CS 1*/
	}else if(spi_num==2){ 
  	MAIN_SPI_CS_H;/*CS 1*/
	}
}

/******************************************************************************
 *函数名：lcd_draw_bmp_pic
 *参  数：pic:要显示的图片的地址
 *返回值：void无
 *作  用：显示图片
 ******************************************************************************/
void lcd_draw_bmp_pic(unsigned char spi_num,unsigned char *pic) {
	unsigned short *src = (unsigned short*)pic;
	unsigned short dst[320*240];
	unsigned char *buf = (unsigned char *)dst;
	int i;
	
	lcd_set_pos(spi_num,0, 319, 0,239);
	if(spi_num==1){      
    PREVIEW_WRX_H;/*数据*/
    PREVIEW_SPI_CS_L; /*CS 0*/
    }else if(spi_num==2){ 
  	MAIN_WRX_H;/*数据*/
  	MAIN_SPI_CS_L;    /*CS 0*/
	}

	for(i=0; i<320*240; i++)
		dst[i] = cpu_to_be16(src[i]);
#if 0
	for(i=0; i<320*240*2; i++)
		do_spi_xfer_val(spi_num, 0, buf[i], 1);
#else
	do_spi_xfer_vals(spi_num, 0, buf, 320*240*2);
#endif
	if(spi_num==1){ 
    PREVIEW_SPI_CS_H;/*CS 1*/
	}else if(spi_num==2){ 
  	MAIN_SPI_CS_H;/*CS 1*/
	}
}

/******************************************************************************
 *函数名：lcd_partial_set
 *参  数：sr:起始行， er:终止行
 *返回值：void无
 *作  用：设置屏幕的部分模式
 ******************************************************************************/
void lcd_partial_set(unsigned char spi_num,unsigned int sr, unsigned int er) {
	spi_write_comd(spi_num,0x30);
	spi_write_data(spi_num,sr>>8);
	spi_write_data(spi_num,sr&0xff);
	spi_write_data(spi_num,er>>8);
	spi_write_data(spi_num,er&0xff);
	spi_write_comd(spi_num,0x12);
}
/*LCD关闭显示*/
void nova_logo_display_off(unsigned char spi_num) {
	spi_write_comd(spi_num,0x28); //display off
}
/*LCD开启显示*/
void nova_logo_display_on(unsigned char spi_num) {
	spi_write_comd(spi_num,0x29); //display on
}



/*SPI 管脚初始化*/
void config_io_lcd_and_io_spi(int spi_num)
{
  if(spi_num==1)
  {
    /*LCD IO控制管脚默认电平配置*/
    MAIN_RST_H;    /*LCD复位管脚*/
    MAIN_BLK_H;    /*LCD背光*/
    MAIN_WRX_H;    /*命/数据选择*/
    /*SPI使用管脚电平默认配置*/
    //MAIN_SPI_CS_H; /*CS  */
    //MAIN_SPI_CLK_H; /*CLK */
    //MAIN_SPI_MISO_H; /*MISO*/
    //MAIN_SPI_MOSI_H; /*MOSI*/
  }else 
  if(spi_num==2)
  {
    /*LCD IO控制管脚默认电平配置*/
    PREVIEW_RST_H;      /*LCD复位管脚*/
    PREVIEW_BLK_H;      /*LCD背光*/
    PREVIEW_WRX_H;      /*命/数据选择*/
    /*SPI使用管脚电平默认配置*/
    //PREVIEW_SPI_CS_H;   /*CS  */
    //PREVIEW_SPI_CLK_H;  /*CLK */
    //PREVIEW_SPI_MISO_H; /*MISO*/
    //PREVIEW_SPI_MOSI_H; /*MOSI*/
  }
}


void lcd_main_init(int id);
void lcd_preview_init(void);
/*初始化LCD,依据SPI挂在的不同，决定初始化的是哪一个LCD
  SPI1--PREVIEW_LCD
  SPI2--MAIN_LCD
*/
void lcd_init(int spi_num)
{
  config_io_lcd_and_io_spi(spi_num);/*设置其IO为默认电平*/
  /*复位芯片*/
  if(spi_num==1)
  {
    /*物理复位*/
    //PREVIEW_RST_H;
    //mdelay(10);    
    PREVIEW_RST_L;
    mdelay(1);    
    PREVIEW_RST_H;
    mdelay(120);    
    PREVIEW_BLK_L;/*灭背光*/
    //lcd_preview_init();/*寄存器初始化*/
	lcd_main_init(spi_num);
  }else 
  if(spi_num==2)
  {
    /*物理复位*/
    //MAIN_RST_H;
    //mdelay(10);    
    MAIN_RST_L;
    mdelay(1);    
    MAIN_RST_H;
    mdelay(120); 
    MAIN_BLK_L;/*灭背光*/
    lcd_main_init(spi_num);/*寄存器初始化*/
  }
  /*关闭显示*/
  
}





#if 0
void lcd_preview_init(void)
{
  spi_write_comd(1,0x0011);//Sleep Out 
  mdelay(120);    
 
  spi_write_comd(1,0x0036); 
  spi_write_data(1,0x0000); 

  spi_write_comd(1,0x003A); 
  spi_write_data(1,0x0056); //65K RGB模式 18bit(666)/pixel   

  spi_write_comd(1,0x00B2); 
  spi_write_data(1,0x000C);   
  spi_write_data(1,0x000C); 
  spi_write_data(1,0x0000); 
  spi_write_data(1,0x0033);     
  spi_write_data(1,0x0033); 

  spi_write_comd(1,0x00B7); 
  spi_write_data(1,0x0061);   

  spi_write_comd(1,0x00BB); 
  spi_write_data(1,0x0031); 

  spi_write_comd(1,0x00C0); 
  spi_write_data(1,0x003C); 

  spi_write_comd(1,0x00C2); 
  spi_write_data(1,0x0001);   
  spi_write_data(1,0x00c3); 

  spi_write_comd(1,0x00C3); 
  spi_write_data(1,0x000B); 

  spi_write_comd(1,0x00C4); 
  spi_write_data(1,0x0020); 

  spi_write_comd(1,0x00C6); 
  spi_write_data(1,0x000F); 
  
  spi_write_comd(1,0xca); 
  spi_write_data(1,0x0f); 
  spi_write_comd(1,0xc8); 
  spi_write_data(1,0x08); 
  spi_write_comd(1,0x55);// 
  spi_write_data(1,0x00);//90 =>00 

  spi_write_comd(1,0x00D0);
  spi_write_data(1,0x00A4);   
  spi_write_data(1,0x00A1); 

/* new gama  --曲线平滑 */
  spi_write_comd(1,0xe0);
  spi_write_data(1,0xF0);//V63 V0
  spi_write_data(1,0x0a);//V1
  spi_write_data(1,0x12);//V2
  spi_write_data(1,0x10);//V4
  spi_write_data(1,0x0b);
  spi_write_data(1,0x19);//J0
  spi_write_data(1,0x37);//V20
  spi_write_data(1,0x54);
  spi_write_data(1,0x4A);//V43
  spi_write_data(1,0x39);//J1
  spi_write_data(1,0x1f);
  spi_write_data(1,0x1f);//V59
  spi_write_data(1,0x30);//V61
  spi_write_data(1,0x3A);//V62

  spi_write_comd(1,0xe1);
  spi_write_data(1,0xF0);//V63 V0
  spi_write_data(1,0x0a);//V1
  spi_write_data(1,0x12);//V2
  spi_write_data(1,0x10);//V4
  spi_write_data(1,0x0b);
  spi_write_data(1,0x19);//J0
  spi_write_data(1,0x32);//V20
  spi_write_data(1,0x44);
  spi_write_data(1,0x4E);//V43
  spi_write_data(1,0x3c);//J1
  spi_write_data(1,0x1f);
  spi_write_data(1,0x1f);//V59
  spi_write_data(1,0x3A);//V61
  spi_write_data(1,0x3F);//V62
  
  spi_write_comd(1,0x00E9); 
  spi_write_data(1,0x0008); 
  spi_write_data(1,0x0008); 
  spi_write_data(1,0x0004); 
   
  spi_write_comd(1,0x00E4); 
  spi_write_data(1,0x0027); 
  spi_write_data(1,0x0000); 
  spi_write_data(1,0x0010); 
  spi_write_comd(1,0x003A); 
  spi_write_data(1,0x0005); 

  spi_write_comd(1,0xB0);
  spi_write_data(1,0x11);
  spi_write_data(1,0xF0);

  spi_write_comd(1,0xB1);
  spi_write_data(1,0xC0);
  spi_write_data(1,0x02);
  spi_write_data(1,0x14);
}
/*SPI2 */
void lcd_main_init(void)
{
  spi_write_comd(2,0x36);  //设置显示模式
  spi_write_data(2,1<<3|0X60);  /*RGB模式*/
  //***************************************************************//LCD SETING
  spi_write_comd(2,0x3A);  //设置接口模式
  spi_write_data(2,0x55);  //65K RGB模式 16bit(565)/pixel 	
  /*--------------------------------ST7789S Frame rate setting----------------------------------*/
  spi_write_comd(2,0xb2);
  spi_write_data(2,0x05);
  spi_write_data(2,0x05);
  spi_write_data(2,0x00);
  spi_write_data(2,0x33);
  spi_write_data(2,0x33);

  spi_write_comd(2,0xb7);
  spi_write_data(2,0x35);
  /*---------------------------------ST7789S Power setting--------------------------------------*/
  spi_write_comd(2,0xbb);
  spi_write_data(2,0x3F);//vcom

  spi_write_comd(2,0xc0);
  spi_write_data(2,0x2C);//

  spi_write_comd(2,0xc2);
  spi_write_data(2,0x01);//

  spi_write_comd(2,0xc3);
  spi_write_data(2,0x0F);//

  spi_write_comd(2,0xc4);
  spi_write_data(2,0x20);

  spi_write_comd(2,0xc6);
  spi_write_data(2,0x0F);

  spi_write_comd(2,0xd0);
  spi_write_data(2,0xa4);
  spi_write_data(2,0xa1);

  spi_write_comd(2,0xe8);
  spi_write_data(2,0x03);

  spi_write_comd(2,0xe9);
  spi_write_data(2,0x09);
  spi_write_data(2,0x09);
  spi_write_data(2,0x08);
  /*--------------------------------ST7789S gamma setting---------------------------------------*/
  /* new gama  --曲线平滑 */
  spi_write_comd(2,0xe0);
  spi_write_data(2,0xF0);//V63 V0
  spi_write_data(2,0x0a);//V1
  spi_write_data(2,0x12);//V2
  spi_write_data(2,0x10);//V4
  spi_write_data(2,0x0b);
  spi_write_data(2,0x19);//J0
  spi_write_data(2,0x37);//V20
  spi_write_data(2,0x54);
  spi_write_data(2,0x4A);//V43
  spi_write_data(2,0x39);//J1
  spi_write_data(2,0x1f);
  spi_write_data(2,0x1f);//V59
  spi_write_data(2,0x30);//V61
  spi_write_data(2,0x3A);//V62

  spi_write_comd(2,0xe1);
  spi_write_data(2,0xF0);//V63 V0
  spi_write_data(2,0x0a);//V1
  spi_write_data(2,0x12);//V2
  spi_write_data(2,0x10);//V4
  spi_write_data(2,0x0b);
  spi_write_data(2,0x19);//J0
  spi_write_data(2,0x32);//V20
  spi_write_data(2,0x44);
  spi_write_data(2,0x4E);//V43
  spi_write_data(2,0x3c);//J1
  spi_write_data(2,0x1f);
  spi_write_data(2,0x1f);//V59
  spi_write_data(2,0x3A);//V61
  spi_write_data(2,0x3F);//V62

  spi_write_comd(2,0x21); //反显

  spi_write_comd(2,0x2A); //Frame rate control
  spi_write_data(2,0x00);
  spi_write_data(2,0x00);
  spi_write_data(2,0x00);
  spi_write_data(2,0xEF);

  spi_write_comd(2,0x2B); //Display function control
  spi_write_data(2,0x00);
  spi_write_data(2,0x00);
  spi_write_data(2,0x01);
  spi_write_data(2,0x3F);

  spi_write_comd(2,0x11);

}
#endif
void lcd_main_init(int id)
{
	spi_write_comd(id,0x11);
	mdelay(120);
  //***************************************************************//LCD SETING
  spi_write_comd(id,0x3A);  //设置接口模式
  spi_write_data(id,0x05);  //65K RGB模式 16bit(565)/pixel 

  spi_write_comd(id,0x36);  //设置显示模式
  spi_write_data(id,0X60);  /*RGB模式*/

  /*--------------------------------ST7789S Frame rate setting----------------------------------*/
  spi_write_comd(id,0xb2);
  spi_write_data(id,0x05);
  spi_write_data(id,0x05);
  spi_write_data(id,0x00);
  spi_write_data(id,0x33);
  spi_write_data(id,0x33);

  spi_write_comd(id,0xb7);
  spi_write_data(id,0x35);
  /*---------------------------------ST7789S Power setting--------------------------------------*/
  //spi_write_comd(id,0xbb);
  //spi_write_data(id,0x22);//vcom

  spi_write_comd(id,0xc0);
  spi_write_data(id,0x2C);//

  spi_write_comd(id,0xc2);
  spi_write_data(id,0x01);//
	spi_write_data(id,0xff);
	
  spi_write_comd(id,0xc3);
  spi_write_data(id,0x0F);//

  spi_write_comd(id,0xc4);
  spi_write_data(id,0x20);

  spi_write_comd(id,0xc5);
  spi_write_data(id,0x20);
  
  spi_write_comd(id,0xc6);
  spi_write_data(id,0x11);

  spi_write_comd(id,0xd0);
  spi_write_data(id,0xa4);
  spi_write_data(id,0xa1);

  spi_write_comd(id,0xe8);
  spi_write_data(id,0x03);

  spi_write_comd(id,0xe9);
  spi_write_data(id,0x09);
  spi_write_data(id,0x09);
  spi_write_data(id,0x08);
  /*--------------------------------ST7789S gamma setting---------------------------------------*/
  /* new gama  --曲线平滑 */
  spi_write_comd(id,0xe0);
  spi_write_data(id,0xF0);//V63 V0
  spi_write_data(id,0x0a);//V1
  spi_write_data(id,0x12);//V2
  spi_write_data(id,0x10);//V4
  spi_write_data(id,0x0b);
  spi_write_data(id,0x19);//J0
  spi_write_data(id,0x37);//V20
  spi_write_data(id,0x54);
  spi_write_data(id,0x4A);//V43
  spi_write_data(id,0x39);//J1
  spi_write_data(id,0x1f);
  spi_write_data(id,0x1f);//V59
  spi_write_data(id,0x30);//V61
  spi_write_data(id,0x3A);//V62

  spi_write_comd(id,0xe1);
  spi_write_data(id,0xF0);//V63 V0
  spi_write_data(id,0x0a);//V1
  spi_write_data(id,0x12);//V2
  spi_write_data(id,0x10);//V4
  spi_write_data(id,0x0b);
  spi_write_data(id,0x19);//J0
  spi_write_data(id,0x32);//V20
  spi_write_data(id,0x44);
  spi_write_data(id,0x4E);//V43
  spi_write_data(id,0x3c);//J1
  spi_write_data(id,0x1f);
  spi_write_data(id,0x1f);//V59
  spi_write_data(id,0x3A);//V61
  spi_write_data(id,0x3F);//V62

  spi_write_comd(id,0x21); //反显

  spi_write_comd(id,0x2A); //Frame rate control
  spi_write_data(id,0x00);
  spi_write_data(id,0x00);
  spi_write_data(id,0x00);
  spi_write_data(id,0xEF);

  spi_write_comd(id,0x2B); //Display function control
  spi_write_data(id,0x00);
  spi_write_data(id,0x00);
  spi_write_data(id,0x01);
  spi_write_data(id,0x3F);

  spi_write_comd(id,0x11);


}









